//
//  CollectionViewCell.m
//  封装轮播图
//
//  Created by 甘霞 on 15/11/23.
//  Copyright © 2015年 甘霞. All rights reserved.
//

#import "CollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "UIImage+GIF.h"
#import "GXSuggestModel.h"

@interface CollectionViewCell ()

@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UIImageView *autorImageView;
@property (nonatomic, retain) UILabel *authorName;
@property (nonatomic, retain) UILabel *subTitleLabel;
@property (nonatomic, retain) UIImageView *pageImageView;
@property (nonatomic, retain) UILabel *pageLabel;

@end

@implementation CollectionViewCell

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:@"pageChange"
                                                  object:nil];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews {
    
    //图片
    self.imageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.imageView];
    
    //标题
    self.titleLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.titleLabel];
    [self.titleLabel setNumberOfLines:2];
    [self.titleLabel setTextColor:[UIColor whiteColor]];
    [self.titleLabel setFont:[UIFont systemFontOfSize:kTitleFontSize]];
    
    //副标题
    self.subTitleLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.subTitleLabel];
    [self.subTitleLabel setNumberOfLines:3];
    [self.subTitleLabel setTextColor:[UIColor whiteColor]];
    [self.subTitleLabel setFont:[UIFont systemFontOfSize:kSubTitleFontSize]];
    
    //作者头像
    self.autorImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.autorImageView];
    
    //作者名字
    self.authorName = [[UILabel alloc] init];
    [self.contentView addSubview:self.authorName];
    [self.authorName setTextColor:[UIColor whiteColor]];
    [self.authorName setFont:[UIFont systemFontOfSize:kSubTitleFontSize]];
    
    //页码图片
    self.pageImageView = [[UIImageView alloc] init];
    [self.pageImageView setImage:[UIImage imageNamed:@"icon-pagersign"]];
    [self.contentView addSubview:self.pageImageView];
    
    //页码
    self.pageLabel = [[UILabel alloc] init];
    [self.pageLabel setTextColor:[UIColor whiteColor]];
    [self.pageLabel setTextAlignment:NSTextAlignmentCenter];
    [self.contentView addSubview:self.pageLabel];
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    
    [self setNeedsDisplay];
    
    //图片
    [self.imageView setFrame:CGRectMake(0, 0, layoutAttributes.size.width, layoutAttributes.size.height)];
    
    CGFloat height = layoutAttributes.size.height - 70;
    
    //文本宽度
    CGFloat labelW = layoutAttributes.size.width - 3 * kAutoSideBorder - kPageImageSize;
    //标题
    [self.titleLabel setFrame:CGRectMake(kAutoSideBorder, height - 150, labelW, 100)];
    
    //作者头像
    [self.autorImageView setFrame:CGRectMake(kAutoSideBorder, height - 50, kAuthorImageViewSize, kAuthorImageViewSize)];
    self.autorImageView.layer.cornerRadius = kAuthorImageViewSize / 2;
    self.autorImageView.layer.masksToBounds = YES;
    
    //作者名字
    [self.authorName setFrame:CGRectMake(kAutoSideBorder + 40, height - 50, labelW, 30)];
    
    //副标题
    [self.subTitleLabel setFrame:CGRectMake(kAutoSideBorder,height - 20,  labelW, 80)];
    
    //页码图片
    [self.pageImageView setFrame:CGRectMake(kAutoSideBorder + labelW + 10, height, kPageImageSize, kPageImageSize)];
    
    //页码
    [self.pageLabel setFrame:CGRectMake(0, 0, kPageImageSize, kPageImageSize)];
    self.pageLabel.center = self.pageImageView.center;
}

#pragma mark - 模型赋值
- (void)setSuggestModel:(GXSuggestModel *)suggestModel {
    
    if (_suggestModel != suggestModel) {
        _suggestModel = suggestModel;
    }
    
    //  注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(pageChange:)
                                                 name:@"pageChange"
                                               object:nil];
    
    //  图片
    [self.imageView sd_setImageWithURL:_suggestModel.thumb[@"raw"]
                      placeholderImage:[UIImage sd_animatedGIFNamed:@"loading"]];
    //  标题
    self.titleLabel.text = _suggestModel.title;
    //  作者头像
    [self.autorImageView sd_setImageWithURL: _suggestModel.cp[@"avatar"]];
    //  作者名字
    self.authorName.text = _suggestModel.cp[@"screen_name"];
    //  副标题
    self.subTitleLabel.text = _suggestModel.content[@"desc"];
}

#pragma mark - 通知中心响应方法
- (void)pageChange:(NSNotification *)noti {
    //  更换页码
    self.pageLabel.text = [NSString stringWithFormat:@"%@/%@", noti.userInfo[@"currentPage"], noti.userInfo[@"totalPage"]];
    //  动画
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.delegate = self;
    rotationAnimation.toValue = [NSNumber numberWithFloat: -M_PI * 0.5];
    rotationAnimation.duration = 1;
    rotationAnimation.repeatCount = 1.0;
    rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    [rotationAnimation setValue:@"rotationAnimation" forKey:@"MyAnimationType"];
    [self.pageImageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

@end
